package com.warren.blog.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
//静态导入

/**
 * @author warren
 */
@Api("book jdbc")
@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/bookList")
    @ApiOperation("book list")
    public List<Map<String, Object>> getDBInfo() {
        String sql = "select * from book";
        List<Map<String, Object>> list_maps = jdbcTemplate.queryForList(sql);
        return list_maps;
    }

    @GetMapping("/addBook")
    @ApiOperation("add book")
    public String addBook() {
        String sql = "insert into hello_springboot.book(name, description, category) values('都市爱情', '美少女爱情', '爱情篇章')";
        jdbcTemplate.execute(sql);
        return "ok";
    }

    @GetMapping("/deleteBook/{id}")
    @ApiOperation("delete book")
    public String deleteBook(@PathVariable("id") int id) {
        String sql = "delete from hello_springboot.book where id = " + id;
        jdbcTemplate.execute(sql);
        return "ok";
    }

    @ApiOperation("update book")
    @GetMapping("/updateBook/{id}")
    public String updateBook(@PathVariable("id") int id) {
        String sql = "update hello_springboot.book set name = ? , description = ? , category =? where id = " + id;
        Object[] objects = new Object[3];
        objects[0] = "小爱同学";
        objects[1] = "小爱同学";
        objects[2] = "小爱同学";
        jdbcTemplate.update(sql, objects);
        return "ok";
    }
}
